﻿#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

#endregion

namespace VisualWebGuiApplication2
{
    public partial class TabkOpen : UserControl
    {
        int lastid = 0;
         Configdb dbconf;
         bool edit = false;
         Tank_Stocks newrecord;
      /*  BindingSource _bsTankOpen;
        GazOnDevEntities db2 = new GazOnDevEntities();*/
        public TabkOpen()
        {
            InitializeComponent();
            this.RightToLeft = Gizmox.WebGUI.Forms.RightToLeft.Yes;

           // _bsTankOpen = new BindingSource(typeof(Tank_Stocks), "");

            txtTankNumber.Leave += new EventHandler(txtTankNumber_Leave);

         Tank_Stocks tanktofix;
         BindingList<invoiceitem> _currentinvoiceitems = new BindingList<invoiceitem>();
         Invoice _currentinvoice = new Invoice();
        
           
      

            using (GazOnDevEntities db= new GazOnDevEntities())
            {
               

                 dbconf= db.Configdbs.FirstOrDefault();
                txtTankNumber.Text = dbconf.tanknumber;
                txtcreatedby.Text = string.Format("{0}", Session["username"]);
                // must icrease id of invoice id  ;
            }
          //  textBox1.Leave += new EventHandler(textBox1_Leave);
           
   
        

          

        }
        public TabkOpen(string tankid)
        {
            InitializeComponent();
            this.RightToLeft = Gizmox.WebGUI.Forms.RightToLeft.Yes;

            // _bsTankOpen = new BindingSource(typeof(Tank_Stocks), "");

            txtTankNumber.Leave += new EventHandler(txtTankNumber_Leave);

            Tank_Stocks tanktofix;
            BindingList<invoiceitem> _currentinvoiceitems = new BindingList<invoiceitem>();
            Invoice _currentinvoice = new Invoice();




            using (GazOnDevEntities db = new GazOnDevEntities())
            {


                dbconf = db.Configdbs.FirstOrDefault();
                txtTankNumber.Text = tankid;
                txtcreatedby.Text = string.Format("{0}", Session["username"]);
                EventArgs ee=new EventArgs();
                txtTankNumber_Leave(txtTankNumber, ee);
                // must icrease id of invoice id  ;
            }
            //  textBox1.Leave += new EventHandler(textBox1_Leave);






        }

        void txtTankNumber_Leave(object sender, EventArgs e)
        {
            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                newrecord = db.Tank_Stocks.FirstOrDefault(x => (x.TankNumber == txtTankNumber.Text));
                if (null == newrecord) return;
                if (newrecord.Id > 0)
                {
                    if (newrecord.TankSystemStatus == "1")
                    {

                        MessageBox.Show("צובר קיים במערכת אתה עובר למצב עריכה ");
                        edit = true;
                    }
                    if (newrecord.TankSystemStatus == "2")
                    {
                        MessageBox.Show("צובר קיים במערכת בסטאטוס המתנה לשיפוץ אי אפשר להביא את הנתונים שלו מכאן נא לגשת לניתוב או לטופס קליטה לשיפוץ ");
                        txtTankNumber.Text = "";
                        txtTankNumber.Focus();
                        return;
                    }
                    if (newrecord.TankSystemStatus == "3")
                    {
                        MessageBox.Show("צובר קיים במערכת אבל הוא בשיפוץ אי אפשר להביא את הנתונים שלו מכאן נא לגשת  לטופס שיפוץ ");
                        txtTankNumber.Text = "";
                        txtTankNumber.Focus();
                        return;
                    }

                }
                else
                {
                    // MessageBox.Show(" אין צובר עם המספר הזה ");
                    // true צובר חדש 
                    edit = false;
                }


                // newrecord = db.Tank_Stocks.FirstOrDefault(x =>( x.TankNumber == txtTankNumber.Text) &&(x.TankSystemStatus=="1"));
                if (null != newrecord)
                {


                    txtTankNumber.Text = newrecord.TankNumber;
                    //comboBox1.SelectedIndex = comboBox1.FindCode(newrecord.type);
                    if (!string.IsNullOrEmpty(newrecord.type))
                    {
                        comboBox1.SelectedIndex = Convert.ToInt32(newrecord.type);
                    }

                    txtdate1.Text = newrecord.Datetime;
                    txtGazCompanyName.Text = newrecord.GazCompanyName;
                    txtGazCompanyAddress.Text = newrecord.GazCompanyAddress;
                    txtGazDistributor.Text = newrecord.GazDistributor;
                    txtnote.Text = newrecord.note;
                    if (string.IsNullOrEmpty(newrecord.cteatedby))
                    {
                        txtcreatedby.Text = string.Format("{0}", Session["username"]);
                    }
                    else
                    {
                        txtcreatedby.Text = newrecord.cteatedby;
                    }

                    if (null != newrecord.Year_of_manufacture)
                    {
                        txtYear_of_manufacture.Text = newrecord.Year_of_manufacture.ToString();
                    }
                    Date_Of_Fix.Text = newrecord.Date_of_Fix;
                    txtmanufacture_name.Text = newrecord.manufacture_name;
                    if (null != newrecord.Tank_Size)
                    {
                        txt_Tank_Size.Text = newrecord.Tank_Size.ToString();
                    }
                    if (null != newrecord.Label_Number)
                    {
                        txtLabel_Number.Text = newrecord.Label_Number.ToString();
                    }
                    txt_tank_fromAddress.Text = newrecord.Address_Tank_From;
                    if (null != newrecord.NumberOfZoklokPort)
                    {
                        txtNumberOfZoklokPort.Text = newrecord.NumberOfZoklokPort.ToString();
                    }
                    if (null != newrecord.tankStatus)
                    {
                        combotankStatus.SelectedIndex = Convert.ToInt32(newrecord.tankStatus);
                    }
                    checkBox1.Checked = (bool)newrecord.TankWithBase_baton;
                    edit = true;
                }
            }
                
            
        }

        private void btnOpenTank_Click(object sender, EventArgs e)
        {
          
           

            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                 newrecord = db.Tank_Stocks.FirstOrDefault(x => (x.TankNumber == txtTankNumber.Text));
                 if (null != newrecord)
                {
                    if (!edit)
                    {
                        MessageBox.Show("צובר קיים אי אפשר להוסיף אותו פעמיים");
                        return;
                    }
                } 




                if (edit)
                {

                    
                }else
                {
                newrecord = new Tank_Stocks();
                }
                newrecord.TankNumber = txtTankNumber.Text;
                newrecord.type = comboBox1.SelectedIndex.ToString();
                newrecord.Datetime = txtdate1.Text;
                newrecord.GazCompanyName = txtGazCompanyName.Text;
                newrecord.GazCompanyAddress = txtGazCompanyAddress.Text;
                newrecord.GazDistributor = txtGazDistributor.Text;
                newrecord.note =txtnote.Text;
                newrecord.cteatedby = txtcreatedby.Text;
                if (!string.IsNullOrEmpty(txtYear_of_manufacture.Text))
                {
                    newrecord.Year_of_manufacture = Convert.ToInt32(txtYear_of_manufacture.Text);
                }
                newrecord.Date_of_Fix = Date_Of_Fix.Text;
                newrecord.manufacture_name = txtmanufacture_name.Text;
                if (!string.IsNullOrEmpty(txt_Tank_Size.Text))
                {
                    newrecord.Tank_Size = txt_Tank_Size.Text;
                }
                if (!string.IsNullOrEmpty(txtLabel_Number.Text))
                {
                newrecord.Label_Number =Convert.ToInt32(txtLabel_Number.Text);
                }
                newrecord.Address_Tank_From = txt_tank_fromAddress.Text;
                if (!string.IsNullOrEmpty(txtNumberOfZoklokPort.Text))
                {
                    newrecord.NumberOfZoklokPort = Convert.ToInt32(txtNumberOfZoklokPort.Text);
                 }
                newrecord.tankStatus = combotankStatus.SelectedIndex.ToString();
                newrecord.TankWithBase_baton = checkBox1.Checked ? true : false;
                // means that enabled at the stock  
                newrecord.TankSystemStatus = "1";

                if (edit)
                {
                    try
                    {
                        db.Attach(newrecord);
                        db.ObjectStateManager.ChangeObjectState(newrecord, EntityState.Modified);
                    }
                    catch (Exception ddd)
                    {

                        db.ApplyOriginalValues("Tank_Stocks", newrecord);
                    }
                }
                else
                {
                    db.AddToTank_Stocks(newrecord);
                    dbconf.tanknumber = (Convert.ToInt32(dbconf.tanknumber) + 1).ToString();
                    db.Attach(dbconf);
                    db.ObjectStateManager.ChangeObjectState(dbconf, EntityState.Modified);
                }

               

               
                  
                if (db.SaveChanges() > 0)
                {
                    MessageBox.Show("פעולת ההוספה הצליחה");
                  
                   
                }
                else
                {

                }
            }
        }
    }
}